-
Notifications
You must be signed in to change notification settings - Fork 276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proxy: improve performance of isStmt functions #10000
Conversation
This commit updates `isStmtBegin`, `isStmtCommit`, and `isStmtRollback` functions to use pre-compiled regex for better performance. Sample benchmark: func isStmtBegin(c []byte) bool { matched, _ := regexp.MatchString(beginPattern, string(c)) return matched } func isStmtBeginNew(c []byte) bool { return beginRegex.Match(c) } func BenchmarkIsStmtBegin(b *testing.B) { for i := 0; i < b.N; i++ { isStmtBegin([]byte{'b', 'e', 'g', 'i', 'n'}) } } func BenchmarkIsStmtBeginNew(b *testing.B) { for i := 0; i < b.N; i++ { isStmtBeginNew([]byte{'b', 'e', 'g', 'i', 'n'}) } } BenchmarkIsStmtBegin-16 68071 22374 ns/op 8528 B/op 116 allocs/op BenchmarkIsStmtBeginNew-16 3735127 280.3 ns/op 5 B/op 1 allocs/op Signed-off-by: Eng Zer Jun <[email protected]>
@Juneezee Thanks for your contributions! This pull request aims to improve the performance of the However, there are a few issues that need to be addressed:
To address these issues, I suggest the following changes:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But seriously, why this is not
strings.TrimSpace().ToUpper().Compare("BEGIN")?
@fengttt Previously we were using a more simpler approach of matching the given This is the current regex for begin statement: |
The regular expression does not really work, how about comment? The true question is that WHY do we need this feature in proxy? |
/cc @volgariver6 , PR reference #9338 |
这里的begin commit rollback 等是用来在proxy中判断是否可以迁移连接的,如果尚处于事务中,就不可以迁移该连接。 |
The we we detect active transaction is not robust enough. Is there anything more reliable from the protocol level? For example, when we plan to migrate a connection, can we issue a command to server to check if current connection is in transaction? Assume connection migration is rare, and probably you HAVE TO migrate no matter what (due to CN crash/congestion etc), why don't we just simply abort current transaction and migrate? |
比如当某个租户做了cn的扩容/缩容,就需要做负载均衡,需要做连接的迁移。 当时实现的时候也感觉这种方式不太好,我改一下吧,看怎么获取当前的事务状态。 |
I filed a bug tracking this. This PR is OK, as it is strictly an improvement. Thank you @Juneezee |
What type of PR is this?
Which issue(s) this PR fixes:
What this PR does / why we need it:
The
regexp.MatchString(pattern, str)
function performs aregexp.Compile(pattern)
operation internally. Since we already know the regex pattern at compile time, we can useregexp.MustCompile
to avoid compiling the same regex pattern for each function invocation.This pull request updates the
isStmtBegin
,isStmtCommit
, andisStmtRollback
functions to use pre-compiled regex patterns, resulting in better performance and less memory allocations.Sample benchmark:
Result: